package com.tencent.wxcloudrun.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencent.wxcloudrun.domain.TActiJoinFill;
import com.tencent.wxcloudrun.dto.ActiJoinFillReqDTO;
import com.tencent.wxcloudrun.mapper.TActiJoinFillMapper;
import com.tencent.wxcloudrun.service.TActiJoinFillService;

/**
 * @author ziyin.zhao
 * @description 针对表【t_acti_join_fill(活动参选预填表)】的数据库操作Service实现
 * @createDate 2025-04-25 09:47:47
 */
@Service
public class TActiJoinFillServiceImpl extends ServiceImpl<TActiJoinFillMapper, TActiJoinFill>
    implements TActiJoinFillService {

    @Override
    public void joinToDb(Long userId, Long actiId, List<ActiJoinFillReqDTO> fillList) {
        if (CollectionUtils.isEmpty(fillList)) {
            return;
        }
        List<TActiJoinFill> actiJoinFillList = new ArrayList<>(fillList.size());
        for (ActiJoinFillReqDTO actiJoinFillReqDTO : fillList) {
            TActiJoinFill actiJoinFill = new TActiJoinFill();
            actiJoinFill.setActiId(actiId);
            actiJoinFill.setUserId(userId);
            actiJoinFill.setFillId(actiJoinFillReqDTO.getFillId());
            actiJoinFill.setFillValue(actiJoinFillReqDTO.getFillValue());
            actiJoinFill.setCreatedBy(String.valueOf(userId));
            actiJoinFill.setCreatedTime(LocalDateTime.now());
            actiJoinFill.setUpdatedBy(String.valueOf(userId));
            actiJoinFill.setUpdatedTime(LocalDateTime.now());
            actiJoinFillList.add(actiJoinFill);
        }
        this.saveBatch(actiJoinFillList);
    }

    @Override
    public void deleteJoin(Long actiId, Long userId) {
        LambdaQueryWrapper<TActiJoinFill> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TActiJoinFill::getActiId, actiId);
        queryWrapper.eq(TActiJoinFill::getUserId, userId);
        queryWrapper.eq(TActiJoinFill::getEnableFlag, 1);
        this.remove(queryWrapper);
    }

}
